<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../ui/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../ui/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="candyToolbar">
            <div class="layui-btn-group">
                <input type="checkbox" name="expandSwitch" lay-skin="switch" lay-text="展开|折叠" checked lay-filter="expand" >
            </div>
        </script>
        <table class="layui-hide" id="candyList" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="addBrother">新增同级</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="addChild">新增下级</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../../ui/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../ui/layuimini/js/lay-config.js"></script>

<script>
    var candyList;
    layui.use(['form', 'table','auth', 'treetable'], function () {
        var $ = layui.jquery,
            form = layui.form;
        var table = layui.table;
        candyList = layui.treetable;
        var api="/api/cms/catalog";
        candyList.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#candyList',
            url: api+'/list',
            parseData: function(res){ //res 即为原始返回的数据
                return {
                  "code": 0, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": res.data.total, //解析数据长度
                  "data": res.data.records //解析数据列表
                };
            },
            toolbar: '#candyToolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '分类列表说明',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: 'numbers'},
                {field: 'name', minWidth: 200, title: '分类名称'},
                {field: 'id', width: 200, title: 'ID', sort: true, hide: true},
                {title: '操作', minWidth: 280, align: "center", fixed:"right", toolbar: '#currentTableBar'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: false
        });
        form.on('switch(expand)', function (obj) {
            if (obj.elem.checked) {
                candyList.expandAll('#candyList');
            } else
            {
                candyList.foldAll('#candyList');
            }
        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'form.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if(obj.event === 'addBrother')
            {
                var index = layer.open({
                    title: '新增['+data.name+']同级分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'form.html?bid='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if(obj.event === 'addChild')
            {
                var index = layer.open({
                    title: '新增['+data.name+']下级分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'form.html?pid='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {
                del(obj.data.id);
            }
        });
        function del(ids) {
            layer.confirm('确认要删除选择的记录吗？', function (index) {
                layer.close(index);
                var settings = {
                    "url": api,
                    "method": "DELETE",
                    "data": {"id":ids}
                };
                $.ajax(settings).done(function (response) {
                    reload();
                });
                return false;
            });
        }
    });
    function reload() {
        location.reload();
    }
</script>

</body>
</html>